﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Threading.Tasks;
using Cef.Utility;
using Newtonsoft.Json;

namespace CefSharp.MinimalExample.WinForms
{
    /// <summary>
    /// js c#回调类
    /// </summary>
    class ScriptCallbackManager
    {
        //public void FindComputerInfo(IJavascriptCallback javascriptCallback)
        //{
        //    Task.Factory.StartNew(async () =>
        //    {
        //        using (javascriptCallback)
        //        {
        //            Computer computer = new Computer();
        //            string response = JsonConvert.SerializeObject(new
        //            {
        //                cpu_id = computer.CPU_Id,
        //                disk_id = computer.Disk_Id,
        //                host_name = computer.HostName,
        //                networkcard = computer.NetworkCard,
        //                serialNumber = computer.SerialNumber_Manufacturer_Product.Item1,
        //                manufacturer = computer.SerialNumber_Manufacturer_Product.Item2,
        //                product = computer.SerialNumber_Manufacturer_Product.Item3,
        //            });
        //            await javascriptCallback.ExecuteAsync(response);
        //        }
        //    });

        //}

        //public void FindComputerResourceUsage(IJavascriptCallback javascriptCallback)
        //{
        //    Task.Factory.StartNew(async () =>
        //    {
        //        using (javascriptCallback)
        //        {
        //            string response = JsonConvert.SerializeObject(SystemInfo.Current);
        //            await javascriptCallback.ExecuteAsync(response);
        //        }
        //    });
        //}

        //public void FindUsbDevices(IJavascriptCallback javascriptCallback)
        //{
        //    Task.Factory.StartNew(async () =>
        //    {
        //        using (javascriptCallback)
        //        {
        //            var devices = new List<UsbDeviceInfo>();

        //            ManagementObjectCollection collection;
        //            using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""))
        //                collection = searcher.Get();

        //            foreach (var device in collection)
        //            {
        //                devices.Add(new UsbDeviceInfo(
        //                    (string)device.GetPropertyValue("DeviceID"),
        //                    (string)device.GetPropertyValue("PNPDeviceID"),
        //                    (string)device.GetPropertyValue("Description")
        //                ));
        //            }

        //            collection.Dispose();

        //            string response = JsonConvert.SerializeObject(devices);
        //            await javascriptCallback.ExecuteAsync(response);
        //        }
        //    });
        //}

        //public void ReadIdCardInfo(IJavascriptCallback javascriptCallback)
        //{
        //    Task.Factory.StartNew(async () =>
        //    {
        //        using (javascriptCallback)
        //        {
        //            string response;
        //            try
        //            {
        //                var result = IdReader.Read();
        //                response = JsonConvert.SerializeObject(result);

        //            }
        //            catch (Exception e)
        //            {
        //                response = JsonConvert.SerializeObject(new {e.Message});
        //            }

        //            await javascriptCallback.ExecuteAsync(response);
        //        }
        //    });
        //}

        public void ReadIdCardInfo(IJavascriptCallback javascriptCallback)
        {
            const int taskDelay = 1500;

            var task = Task.Factory.StartNew(() =>
            {
                using (javascriptCallback)
                {
                    //NOTE: Classes are not supported, simple structs are
                    var response = JsonConvert.SerializeObject("This callback from C# was delayed " + taskDelay + "ms");
                    javascriptCallback.ExecuteAsync(response);
                }
            });

            task.Wait();

        }

    }
}